#include "mapwindow.h"

MapWindow::MapWindow(QWidget* parent)
    :
    GLWindow(parent),
    myTileSheet(parent),
    myCamera(0,0,640, 480),
    myHorizontalScroll( Qt::Horizontal, this),
    myVerticalScroll( Qt::Vertical, this)


{
    myVerticalScroll.setTracking(true);
    myHorizontalScroll.setTracking(true);
    connect(&myVerticalScroll, SIGNAL(valueChanged(int)), this, SLOT(MoveCameraY(int)));
    connect(&myHorizontalScroll, SIGNAL(valueChanged(int)), this, SLOT(MoveCameraX(int)));

    myVerticalScroll.setMaximum(1);
    myVerticalScroll.resize(20, 480);
    myHorizontalScroll.setMaximum(1);
    myHorizontalScroll.resize(620, 20);


    myHorizontalScroll.move(0, 460);
    myVerticalScroll.move(620,0);

    myHorizontalScroll.show();
    myVerticalScroll.show();


    myTileSheet.resize(160, 480);
    myTileSheet.move(640, 20);
    myTileSheet.show();
    myNewBox =NULL;
    myTileSize = 32;
}


MapWindow::~MapWindow()
{
    if(myNewBox) delete myNewBox;
}


void MapWindow::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glLoadIdentity();
    glClearColor(170.f/255.f, 170.f/255.f, 170.f/255.f, 0.f);


    myLevel.Render(myCamera.left(), myCamera.top()); // i guess i cant translate it so whateve




    /// @todo only update the quad tree when the map is loaded or made new because it is
    /// made for the whole map
}






void MapWindow::paintEvent(QPaintEvent* e)
{
    QGLWidget::paintEvent(e);
    if(myNewBox != NULL)
    {
        if(myNewBox->isAccepted())
        {
            if(myNewBox->GetTileName().isEmpty() == false)
            {
                updateGL();
                myLevel.SetImageDir(myNewBox->GetTileName().toStdString());
                myTileSheet.SetImage(myNewBox->GetTileName().toStdString());
                updateGL();
            }

            myLevel.New(myNewBox->GetWidth(), myNewBox->GetHeight(), myNewBox->GetMapName().toStdString());
            myHorizontalScroll.setMaximum(myLevel.GetWidth());
            myVerticalScroll.setMaximum(myLevel.GetHeight());
            myTileSize = myNewBox->GetTileSize();
            myTileSheet.SetSize(myTileSize);
            myLevel.SetSize(myTileSize);


            updateGL();

            delete myNewBox;
            myNewBox = NULL;
        }
        else if(myNewBox->isDestroyed())
        {
            delete myNewBox;
            myNewBox = NULL;
        }
    }
}

void MapWindow::mouseMoveEvent(QMouseEvent* clicked)
{
    if(myLevel.IsAllocated())
    {
        updateGL();
        myLevel.PlaceTile(clicked->x() - myCamera.left(), clicked->y() - myCamera.top(), 2, myTileSheet);
    }
}


void MapWindow::initializeGL()
{
    glClearColor(0.f,0.f,0.f,0.f);
    glEnable(GL_TEXTURE_2D);

    myLevel.SetImageDir("cool.png");
}

void MapWindow::Load(){
    QString filename = QFileDialog::getOpenFileName(0, "Load a Map", "", "*.map");
    updateGL();
    myLevel.Load(filename.toStdString(), myTileSheet);
    myHorizontalScroll.setMaximum(myLevel.GetWidth()*4);
    myVerticalScroll.setMaximum(myLevel.GetHeight()*4);
    updateGL();
}

void MapWindow::Save(){
    myLevel.Save(myTileSheet);

}

void MapWindow::New(){

    if(myNewBox) delete myNewBox;
    myNewBox = new NewBox(this);
    myNewBox->show();
    /// @todo if i create a new box delete and reinstancate the QuadTree to fit the level
}

void MapWindow::LoadTile(std::string imagedir){
    myLevel.SetImageDir(imagedir);
    myTileSheet.SetImage(imagedir);
    /// @todo the same as above
}

void MapWindow::MoveCameraX(int x)
{
    myCamera.setLeft(-x*8);
    updateGL();
}

void MapWindow::MoveCameraY(int y)
{
    myCamera.setTop(-y*8);
    updateGL();
}


void MapWindow::ShowLayer(int layer, bool show)
{
    myLevel.ShowLayer(layer, show);
    updateGL();
}

void MapWindow::SetCurrentLayer(int c)
{
    myLevel.SetCurrentLayer(c);
}
